<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"  @submit.native.prevent @keyup.enter.native="handleQuery">
      <el-form-item label="指标" prop="qKey">
        <el-input v-model="queryParams.qKey" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item label="指标类型" prop="qType">
        <el-select v-model="queryParams.qType" clearable placeholder="">
          <el-option
            v-for="dict in dict.type.quota_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择类型" prop="qType1">
        <el-select v-model="queryParams.qType1" clearable placeholder="">
          <el-option
            v-for="dict in dict.type.choose_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['userConfig:ucQuoQuota:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
      <el-table-column label="指标" align="center" prop="name"/>
      <el-table-column label="指标类型" align="center" prop="quotaType" sortable="custom">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.quota_type" :value="scope.row.quotaType"/>
        </template>
      </el-table-column>
      <el-table-column label="选择类型" align="center" prop="chooseType" sortable="custom">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.choose_type" :value="scope.row.chooseType"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['userConfig:ucQuoQuota:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="指标" prop="name" style="width: 90%">
          <el-input type="textarea" v-model="form.name" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="指标类型" prop="quotaType">
          <el-radio-group v-model="form.quotaType" size="small">
            <el-radio-button
              v-for="dict in dict.type.quota_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="选择类型" prop="chooseType">
          <el-radio-group v-model="form.chooseType" size="small">
            <el-radio-button
              v-for="dict in dict.type.choose_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio-button>
          </el-radio-group>
        </el-form-item>


        <el-row v-if="form.ucQuoQuotaOptions && (form.chooseType ==2 ||form.chooseType ==3)">
          <el-col :span="24">
            <el-form-item label="项目">
              <el-button size="mini" circle class="el-icon-plus" @click="addOption"></el-button>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-for="(item,index) in form.ucQuoQuotaOptions" :key="index">
            <el-col :span="18">
              <el-form-item label="" :prop="`ucQuoQuotaOptions[${index}].name`">
                <el-input v-model="form.ucQuoQuotaOptions[index].name" placeholder=""></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="1" style="height: 1px;"></el-col>
            <el-col :span="4">
              <el-button size="mini" circle class="el-icon-minus" @click="removeOption(index)"></el-button>
            </el-col>
          </el-col>

        </el-row>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>


export default {
  name: "UcQuoQuota",
  dicts: ['quota_type','choose_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        qKey: "",
        qType: undefined,
        qType1: undefined,
        pageNum: 1,
        pageSize: 10,
        isAsc:undefined,
        orderByColumn:undefined
      },
      // 表单参数
      form: {
        id: undefined,
        name: undefined,
        quotaType: undefined,
        chooseType: 3,
        ucQuoQuotaOptions: [{
          name: undefined,
        }],
      },
      // 表单校验
      rules: {
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        quotaType: [{ required: true, message: "请选择", trigger: "blur" }],
        chooseType: [{ required: true, message: "请选择", trigger: "blur" }],
      },
      dialogTableVisible:false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      this.request({
        url: '/userConfig/ucQuoQuota/list',
        method: 'get',
        params: this.queryParams
      }).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.isAsc = undefined;
      this.queryParams.orderByColumn = undefined;
      this.queryParams.pageNum = 1;
      this.$refs.tables.clearSort();
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = this.getLowLineCase(column.prop);
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        quotaType: undefined,
        chooseType: 3,
        ucQuoQuotaOptions: [{
          name: undefined,
        }],
      }
      this.resetForm("form");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加指标";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.request({
        url: '/userConfig/ucQuoQuota/info/' + id,
        method: 'get'
      }).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改指标";

      });
    },
    /** 提交按钮 */
    submitForm: function() {
      let ucQuoQuotaOptionList = this.form.ucQuoQuotaOptions.filter(ucQuoQuotaOption =>{
        return ucQuoQuotaOption.name && ucQuoQuotaOption.name.trim()!='';
      })
      if((this.form.chooseType == 2 || this.form.chooseType == 3) && (!ucQuoQuotaOptionList || ucQuoQuotaOptionList.length <=0)){
        this.$message.error("选项未填写")
      }else{
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (!this.form.id) {
              this.request({
                url: '/userConfig/ucQuoQuota/add',
                method: 'post',
                data: this.form
              }).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            } else {
              this.request({
                url: '/userConfig/ucQuoQuota/edit',
                method: 'post',
                data: this.form
              }).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      }

    },
    addOption(){
      this.form.ucQuoQuotaOptions.push({
        name: undefined,
      })
    },
    removeOption(index){
      if(!this.form.ucQuoQuotaOptions || this.form.ucQuoQuotaOptions.length<=1){
        this.$message.error("最少保留一个")
      }else{
        this.form.ucQuoQuotaOptions.splice(index,1);
      }
    }
  }
};
</script>

